#!/bin/bash

# RISC-V OS 一键上传到 Gitee 脚本
# 作者: 余摩释
# 用法: ./upload.sh [提交信息]

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# 打印带颜色的消息
print_info() {
    echo -e "${BLUE}[信息]${NC} $1"
}

print_success() {
    echo -e "${GREEN}[成功]${NC} $1"
}

print_warning() {
    echo -e "${YELLOW}[警告]${NC} $1"
}

print_error() {
    echo -e "${RED}[错误]${NC} $1"
}

# 检查是否在正确的目录
check_directory() {
    if [ ! -f "Makefile" ] || [ ! -d "kernel" ]; then
        print_error "请在 riscv-os 项目根目录中运行此脚本！"
        exit 1
    fi
}

# 检查 Git 状态
check_git_status() {
    if ! git status &>/dev/null; then
        print_error "当前目录不是 Git 仓库！"
        exit 1
    fi
}

# 清理编译文件
clean_build() {
    print_info "清理编译文件..."
    if [ -f "Makefile" ]; then
        make clean &>/dev/null
        print_success "编译文件清理完成"
    else
        print_warning "未找到 Makefile，跳过清理"
    fi
}

# 编译测试
test_build() {
    print_info "测试编译..."
    if make &>/dev/null; then
        print_success "编译测试通过"
        make clean &>/dev/null  # 编译成功后再次清理
    else
        print_error "编译失败！请检查代码后重试"
        exit 1
    fi
}

# 检查文件变更
check_changes() {
    if git diff-index --quiet HEAD --; then
        print_warning "没有检测到文件变更"
        read -p "是否继续提交？(y/N): " -n 1 -r
        echo
        if [[ ! $REPLY =~ ^[Yy]$ ]]; then
            print_info "取消上传"
            exit 0
        fi
    else
        print_info "检测到以下文件变更："
        git status --porcelain | while read line; do
            echo "  $line"
        done
    fi
}

# 获取提交信息
get_commit_message() {
    if [ -n "$1" ]; then
        COMMIT_MSG="$1"
    else
        echo
        print_info "请输入提交信息："
        read -p "提交信息: " COMMIT_MSG
        
        if [ -z "$COMMIT_MSG" ]; then
            COMMIT_MSG="Update: $(date '+%Y-%m-%d %H:%M:%S')"
            print_warning "使用默认提交信息: $COMMIT_MSG"
        fi
    fi
}

# 执行 Git 操作
do_git_operations() {
    print_info "添加文件到暂存区..."
    git add .
    
    if [ $? -eq 0 ]; then
        print_success "文件添加成功"
    else
        print_error "文件添加失败"
        exit 1
    fi
    
    print_info "提交更改..."
    git commit -m "$COMMIT_MSG"
    
    if [ $? -eq 0 ]; then
        print_success "提交成功"
    else
        print_error "提交失败"
        exit 1
    fi
    
    print_info "推送到 Gitee..."
    git push
    
    if [ $? -eq 0 ]; then
        print_success "推送成功！"
        echo
        print_info "项目地址: https://gitee.com/yu-moshi/riscv-os"
    else
        print_error "推送失败，请检查网络连接和权限"
        exit 1
    fi
}

# 显示使用说明
show_usage() {
    echo "使用方法:"
    echo "  $0                    # 交互式上传"
    echo "  $0 \"提交信息\"        # 直接指定提交信息"
    echo
    echo "示例:"
    echo "  $0 \"修复 UART 驱动 bug\""
    echo "  $0 \"添加新功能\""
}

# 主函数
main() {
    echo
    echo "=========================================="
    echo "    RISC-V OS 一键上传脚本"
    echo "=========================================="
    echo
    
    # 显示帮助信息
    if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
        show_usage
        exit 0
    fi
    
    # 执行检查和操作
    check_directory
    check_git_status
    clean_build
    test_build
    check_changes
    get_commit_message "$1"
    
    echo
    print_warning "即将执行以下操作："
    echo "  1. 添加所有文件到 Git"
    echo "  2. 提交: $COMMIT_MSG"
    echo "  3. 推送到 Gitee"
    echo
    read -p "确认继续？(Y/n): " -n 1 -r
    echo
    
    if [[ $REPLY =~ ^[Nn]$ ]]; then
        print_info "取消上传"
        exit 0
    fi
    
    do_git_operations
    
    echo
    print_success "所有操作完成！"
}

# 运行主函数
main "$@"
